<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of DeLP
 *
 * @author cbriguez
 */
class DeLP {
    //put your code here
    protected $URL_WS_DELP = "http://lidia.cs.uns.edu.ar/ws_delp/DeLPService.php?wsdl";
    
    protected $dbhost = 'localhost';
    protected $dbuser = 'root';
    protected $dbpass = '';
    protected $dbname = 'news';
    
    protected static $delp_code = " 
    %% DEFEASIBLE RULES
    trust_report(V,R) -< report_source(R,S),trust_source(V,S).
    ~trust_report(V,R)-< report_source(R,S),~trust_source(V,S).
    trust_report(V,R)-< trust_viewer(V,V1),trust_report(V1,R).
    ~trust_report(V,R)-< trust_viewer(V,V1),~trust_report(V1,R).
    trust_source(V,S)-< trust_viewer(V,V1),trust_source(V1,S).
    ~trust_source(V,S)-< trust_viewer(V,V1),~trust_source(V1,S).
    trust_report(V,R) -< report_source(R,S),trust_source(V,S),trust_viewer(V,V1),~trust_report(V1,R).
    ~trust_report(V,R)-< report_source(R,S),~trust_source(V,S),trust_viewer(V,V1),trust_report(V1,R).

    %%FACTS
    ";    
    
    protected static $delp_code_loaded = false;
    

    public function connect(){
        
        $conn = mysql_connect($this->dbhost, $this->dbuser, $this->dbpass) or die ('Error connecting to mysql');
        mysql_select_db($this->dbname);
        
    }

    public function addTrustSources(){
        
//        Yii::log('Antes addTrustSources', 'error'); 
        
        $query_trust_sources = "SELECT id_user, trust_source.id_source,username, shortname, rank FROM trust_source
                                LEFT JOIN news_users ON trust_source.id_user = news_users.userid 
                                LEFT JOIN source ON  trust_source.id_source =  source.id_source; " ; 

        $result = mysql_query($query_trust_sources) or die('Error, query 1 failed');
        $source_trust= mysql_fetch_array($result);
        while ($source_trust) {
                // Add to DeLP code
                $eval = ($source_trust['rank'] == 2) ? "":"~"; 
                $this->trust_source($source_trust['username'],$source_trust['shortname'],$eval) ;
                $source_trust = mysql_fetch_array($result);

        }
        
//        Yii::log('Despues addTrustSources', 'error');
        
    }

    public function addTrustUsers(){

//        Yii::log('Antes addTrustUsers', 'error');
        
        $query_trust_users = "SELECT users1.username as 'user_1', users2.username as 'user_2', rank FROM trust_user
                              inner join news_users as users1 on trust_user.id_user1 = users1.userid
                              inner join news_users as users2 on trust_user.id_user2 = users2.userid; ";

        $result = mysql_query($query_trust_users) or die('Error, query 2 failed');
        $user_trust = mysql_fetch_array($result);
        while ($user_trust) {
                // Add to DeLP code
                $eval = ($user_trust['rank'] == 2) ? "":"~"; 
                $this->trust_user($user_trust['user_2'],$user_trust['user_1'],$eval) ;
                $user_trust = mysql_fetch_array($result);

        }
        
//        Yii::log('Despues addTrustUsers', 'error');
        
    }

    function addTrustReports(){

//        Yii::log('Antes addTrustReports', 'error');
        
        $query_trust_reports = "SELECT id_user, trust_report.id_report,username, rank FROM trust_report
                                LEFT JOIN news_users ON trust_report.id_user = news_users.userid ; " ;

        $result = mysql_query($query_trust_reports) or die('Error, query 3 failed');
        $report_trust = mysql_fetch_array($result);
        while ($report_trust) {
                // Add to DeLP code
                $eval = ($report_trust['rank'] == 2) ? "":"~"; 
                $this->trust_report($report_trust['username'],$report_trust['id_report'],$eval) ;
                $report_trust = mysql_fetch_array($result);

        }
        
//        Yii::log('Despues addTrustReports', 'error');
        
    }
    
    function addReportsSource(){
        
//        $query_reports_source = "SELECT items.id, source.shortname FROM items, source 
//                                 WHERE source.name = items.fuente; ";

//        $query_reports_source = "SELECT items.id, source.shortname FROM items LEFT JOIN source 
//                                 ON items.fuente = source.name;";
        
        $query_reports_source = "SELECT items.*, source.shortname FROM items LEFT JOIN source 
                                 ON items.fuente = source.name;";
        
        $result = mysql_query($query_reports_source) or die('Error, query 4 failed');
        $report_source = mysql_fetch_array($result);
        while ($report_source) {
                // Add to DeLP code
                if ($report_source['shortname'] != ''){// este control es solo por ahora, porque la BD no es consistente
                    
                    $this->report_source($report_source['id'], $report_source['shortname']);
                    
                }
                $report_source = mysql_fetch_array($result);

        }
        
    }

    protected function trust_user($user1,$user2,$eval)
    {	
        DeLP::$delp_code .= $eval."trust_viewer(".$user1.",".$user2.")<- true.\n";
    }

    protected function trust_report($user,$report,$eval)
    {     
        DeLP::$delp_code .= $eval."trust_report(".$user.",".$report.")<- true.\n";
    }

    protected function trust_source($user,$source,$eval)
    {	
        DeLP::$delp_code .= $eval."trust_source(".$user.",".$source.")<- true.\n";
    }

    protected function report_source($report,$source)
    {
        DeLP::$delp_code .= "report_source(".$report.",".$source.")<- true.\n";
    }

// --------------------------------- Web Service ---------------------------------

    protected function call_web_service($query,$code,$id) {

        $client = new soapclient($this->URL_WS_DELP);            
        $result = $client->__soapcall('DeLPService', array('query' => $query, 'delp_code'=> $code, 'user_id'=> $id));
        $respuesta['result'] = $result;

        return $respuesta;
    }

    public function callDeLP($user,$article)
    {	
        $id = 'news';
        $query = "trust_report($user,$article)";
        
        if (!DeLP::$delp_code_loaded){
            $this->connect();
            $this->addTrustSources();
            $this->addTrustUsers();
            $this->addReportsSource();
            $this->addTrustReports();
            DeLP::$delp_code_loaded = true;
            
            Yii::log(DeLP::$delp_code, 'error');
            
        }
        
        $answer = $this->call_web_service($query,DeLP::$delp_code,$id);
        return $answer['result'];
    }

// --------------------------------- Web Service ---------------------------------

}

?>
